/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateful;

import entity.CustomerEntity;
import entity.PersonEntity;
import entity.SalesInquiryEntity;
import entity.SalesLeadEntity;
import java.lang.Integer;
import java.lang.Long;
import java.util.ArrayList;
import java.util.HashMap;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author edd
 */
@Stateless
@LocalBean
public class SalesInquirySessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    EntityManager em; 
    SalesInquiryEntity salesInquiry;
    PersonEntity person;
    SalesLeadEntity salesLead;
    CustomerEntity customer;
    
    public SalesInquirySessionBean() {
        
    }
    
    public void addSalesInquiry(Long personId, HashMap<Long,Integer> productQuantity, 
            double indicativePrice) throws Exception {
        
        person = em.find(PersonEntity.class, personId);
        if(person == null)
            throw new Exception("Sales Lead or Customer does not exist.");
        else {
            //salesInquiry = new SalesInquiryEntity(productQuantity, indicativePrice);
            customer = em.find(CustomerEntity.class, personId);
            if(customer != null) {
                salesInquiry.setCustomer(customer);
            }
            else {
                salesLead = em.find(SalesLeadEntity.class, personId);
                salesInquiry.setSalesLead(salesLead);
            }
            em.persist(salesInquiry);
        }
        
    }
    
    public void editSalesInquiry(Long salesInquiryId, HashMap<Long, Integer> productQuantity, 
            double indicativePrice) throws Exception {
        
        salesInquiry = em.find(SalesInquiryEntity.class, salesInquiryId);
        if(salesInquiry == null) 
            throw new Exception("Sales Inquiry does not exist.");
        else {
            //salesInquiry.setProductQuantity(productQuantity); //allow to edit?
            //salesInquiry.setIndicativePrice(indicativePrice);
            em.persist(salesInquiry);
        }
      
    }
    
    public void deleteSalesInquiry(Long salesInquiryId) throws Exception {
        
        salesInquiry = em.find(SalesInquiryEntity.class, salesInquiryId);
        if(salesInquiry == null)
            throw new Exception("Sales Inquiry does not exist.");
        else if(salesInquiry.isArchived())
            throw new Exception("Sales Inquiry has already been deleted.");
        else {
            salesInquiry.setArchived(true);
            em.persist(salesInquiry);
        }
    }
    
    public ArrayList<SalesInquiryEntity> viewSalesInquiries() {
        
        Query query = em.createQuery("SELECT s FROM SalesInquiry s WHERE s.archived = :notArchived"
                + "ORDER BY s.id desc");
        query.setParameter("notArchived", "false");
        
        ArrayList listSalesInquiries = new ArrayList<SalesInquiryEntity>();
        
        for(Object o : query.getResultList()) {
            SalesInquiryEntity s = (SalesInquiryEntity) o;
            listSalesInquiries.add(s);
        }
        return listSalesInquiries;
    }
    
    //Search sales inquiry by sales inquiry id, customer id, customer name, company name, product id
    
}
